package cn.edu.swu.picture;

import com.fasterxml.jackson.databind.DeserializationFeature;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.awt.print.Book;
import java.util.List;

import cn.edu.swu.core.BasicServlet;
import cn.edu.swu.core.JsonResponseWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

public class PictureApi extends BasicServlet{

    private PictureService pictureService = null;

    public void init(ServletConfig config) {
        PictureRepo repo = new PictureRepo();
        pictureService = new PictureService();
        pictureService.setRepo(repo);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        this.methodRoute(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        this.methodRoute(request, response);
    }

    private void methodRoute(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        String uri = request.getRequestURI();
        System.out.println(uri);
        String method = uri.substring(uri.lastIndexOf("/") + 1);
        System.out.println(method);
        if (method.equals("list")) {
            this.list(request, response);
        } else if (method.equals("add")) {
            this.add(request, response);
        }
    }

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException{
        try {
            List<Picture> pictures = this.pictureService.getAll();
            this.responseJson(response, true, pictures);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void responseJson(HttpServletResponse response, boolean b, List<cn.edu.swu.picture.Picture> pictures) {
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException{
        try {
            String json = this.getRequestBody(request);
            System.out.println(json);
            objectMapper.setDateFormat(dateFormatter);
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            Picture picture = objectMapper.readValue(json, Picture.class);
            this.pictureService.save(picture);
            this.responseJson(response, true, picture);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
